{extend name="../../base/view/common/base" /}
{block name="breadcrumb"}
<span class="layui-breadcrumb">
  <a href="https://www.gougucms.com/" target="_blank">勾股DEV</a>
  <a><cite>知识库</cite></a>
</span>
{/block}
<!-- 主体 -->
{block name="body"}
<div class="table-content p-3">
	<div class="layui-tab knowledge-tab" style="margin:0" lay-filter="docDemoTabBrief">
		<ul class="layui-tab-title">
			<li>个人知识库</li>
			<li class="layui-this">共享知识库</li>
		</ul>
	</div>
	<form class="layui-form layui-form-bar border-x border-b mb-3">
		<div class="layui-input-inline" style="width:120px">
			<select name="cate_id">
				<option value="">请选择分类</option>
				{volist name=":set_recursion(knowledge_cate())" id="v"}
				<option value="{$v.id}">{$v.title}</option>
				{/volist}
			</select>
		</div>
		<div class="layui-input-inline" style="width:240px;">
			<input type="text" name="keywords" placeholder="标题/分类/描述" class="layui-input" autocomplete="off" />
		</div>
		<button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">提交搜索</button>
		<span style="float:right" class="layui-btn layui-btn-normal add-new">+ 新建知识库</span>
	</form>
	<div class="knowledge-list layui-row layui-col-space16" id="knowledgeContent">

	</div>
	<div id="pageBox" class="page-box py-4"></div>
</div>

<script type="text/html" id="barDemo">
<div class="layui-btn-group"><span class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view">查看</span></div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
	const eid = {$eid};
	const kid = {$kid};
	var getKnowledge = null;
	var getData = {'page':1,'share':1};
	function init(layui) {
		var gougu = layui.gougu,
			form = layui.form,
			element = layui.element,
			laypage = layui.laypage;
			
		element.on('tab(docDemoTabBrief)', function(data){
			getData.share=data.index;
			getKnowledge();
		});

		if(eid>0 && kid == 0){
			gougu.open('/knowledge/index/add/id/'+kid+'.html');
		}
		if(eid == 0 && kid > 0){
			gougu.open('/knowledge/index/view/id/'+kid+'.html');
		}		
		getKnowledge = function(){
			gougu.get("/knowledge/index/index",getData,function(res){
				if(res.code == 0){
					let card='';
					for(let i=0; i<res.data.length;i++){
						let item = res.data[i];
						card+=`
							<div class="layui-col-md4">
								<div class="layui-card">
									<div class="card-body">
										<div class="meta-title text-overflow1"><a href="/knowledge/index/doc_detail/kid/${item.id}">${item.title}</a></div>
										<div class="meta-desc text-overflow3"><a href="/knowledge/index/doc_detail/kid/${item.id}">${item.desc}</a></div>
									</div>
									<div class="card-actions">
										<ul>
											<li title="收藏数"><i class="layui-icon layui-icon-star-fill"></i>0</li>
											<li title="查看数"><i class="layui-icon layui-icon-friends"></i>${item.views}</li>
											<li title="编辑"><a class="action-edit" data-id="${item.id}" href="javascript:;"><i class="layui-icon layui-icon-edit"></i>编辑</a></li>
											<li title="浏览详情"><a class="action-view" href="/knowledge/index/doc_detail/kid/${item.id}"><i class="layui-icon layui-icon-link"></i>查看</a></li>
										</ul>
									</div>
								</div>
							</div>
							`;
					}
					if(card == ''){
						card='<div style="text-align:center; color:#999; padding:24px 0">暂无数据</div>';
					}
					$('#knowledgeContent').html(card);
					if(getData.page==1 && res.data.length>9){
						laypage.render({
							elem: 'pageBox'
							,limit:9
							,count: 50 //数据总数，从服务端得到
							,jump: function(obj, first){
								console.log(obj.curr); //得到当前页		
								getData.page=obj.curr;
								getKnowledge();
								//首次不执行
								if(!first){
								  //do something
								}
							}
						});
					}
				}		
			});
		}
		getKnowledge();

		$('.add-new').on('click',function(){
			gougu.open('/knowledge/index/add');
		});
		$('#knowledgeContent').on('click','.action-edit',function(){
			let id=$(this).data('id');
			gougu.open('/knowledge/index/view/id/'+id+'.html');
		});
		
		//监听搜索提交
		form.on('submit(webform)', function(data) {
			getData.page=1;
			getData.keywords=data.field.keywords;
			getData.cate_id=data.field.cate_id;
			getKnowledge();
			return false;
		});
	}
</script>
{include file="../../base/view/common/layui" base="base" extend="['gougu','treeGrid']" callback="init" /}
{/block}
<!-- /脚本 -->
